import { getCartByToken } from '@/api/cart'
import { getToken } from '@/utils/auth'

const mutations = {
  SET_CART_COUNT: (state, cartCount) => {
    state.cartCount = cartCount
  },
}

const actions = {
  getCart({ commit }) {
    return new Promise((resolve, reject) => {
      getCartByToken(getToken()).then(response => {
        const { data } = response
        if (!data) {
          return reject('验证失败，请重新登录。')
        }
        commit('SET_CART_COUNT', data.length || 0)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },
}

export default {
  namespaced: true,
  mutations,
  actions
}

